 /*
 * @description: 页面基本模版
 * @Author: along
 * @Date: 2020-00-00
 * @Last Modified by: along
 * @Last Modified time: 2020-00-00
 */
<template>
    <div class="zsdxPopupCode">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">弹窗位置:</p>
                    <el-select
                        v-model="formData.popup_pos"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in posList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">触发条件:</p>
                    <el-select
                        v-model="formData.condition"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in condList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">等级要求:</p>
                    <el-select
                        v-model="formData.leavel"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in leavelList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="$refs.popupDialog.show({type: 'add'})"
                    >
                        新增弹窗
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zsdxPopupCode"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="image_url"
                label="二维码图片"
                align="left"
            >
                <template slot-scope="{row}">
                    <div
                        :style="{
                            width: '70px',
                            height: '70px',
                            margin: '8px 0',
                            backgroundImage: `url(${row.image_url})`,
                            backgroundSize: 'cover',
                            cursor: 'pointer'
                        }"
                        @click="fnShowPreview(row.image_url)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="scope"
                label="位置"
                align="left"
            >
                <template slot-scope="{row}">
                    {{ row.scope | filtertype }}
                </template>
            </el-table-column>
            <el-table-column
                prop="require_stuagent_level"
                label="等级要求"
                align="left"
            >
                <template slot-scope="{row}">
                    {{ row.require_stuagent_level | filterLeavel }}
                </template>
            </el-table-column>
            <el-table-column
                prop="event_type"
                label="触发条件"
                align="left"
            >
                <template slot-scope="{row}">
                    {{ row.event_type == 1 ? '等级提升' : row.event_type === 0 ? '直接展示' : '' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="修改时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="在线状态"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnShowStatus(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="verify"
                label="审核状态"
                align="left"
            >
                <template slot-scope="{row}">
                    <div class="__flex">
                        <p
                            class="__flex_status"
                            :style="{background: row.verify === 0 ? 'rgba(245, 166, 35, 1)' : row.verify === 1 ? 'rgba(76, 217, 100, 1)' : 'rgba(255, 48, 48, 1)'}"
                        />
                        <p>{{ row.verify | filterVerfy }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="verify_fail_reason"
                label="不通过原因"
                align="left"
            >
                <template slot-scope="{row}">
                    {{ row.verify_fail_reason ? row.verify_fail_reason : '--' }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                fixed="right"
                :width="210"
            >
                <template slot-scope="{row}">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="$refs.dialogSuccess.showDialog(row)"
                        :disabled="row.verify === 1"
                    >
                        通过
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="confrimReject(row)"
                        :disabled="row.verify === -1"
                    >
                        不通过
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="$refs.popupDialog.show({type: 'edit', row: row})"
                        :disabled="row.verify === 1"
                    >
                        更换
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        :style="{color: 'rgba(249, 91, 86, 1)'}"
                        @click="$refs.dialogDelete.showDialog(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />

        <!-- 删除弹窗 -->
        <dialog-delete
            ref="dialogDelete"
            :url="'http://cdn.zsdx.cn/phaadmin/dialog-waring.png'"
            :title="'删除提示'"
            :content="'确定是否删除该二维码弹窗?'"
            @confrim="confrimDelete"
        />

        <!-- 通过弹窗 -->
        <dialog-success
            ref="dialogSuccess"
            :url="'http://cdn.zsdx.cn/phaadmin/dialog-waring.png'"
            :title="'温馨提示'"
            :content="'确定通过该二维码弹窗?'"
            @confrim="confrimSuccess"
        />

        <!-- 新增编辑二维码弹窗 -->
        <popup-dialog
            ref="popupDialog"
            @confirm="confirmDialog"
        />
    </div>
</template>

<script>
import dialogDelete from '@/components/dialog/index.vue';
import dialogSuccess from '@/components/dialog/index.vue';
import imgPreview from '@/components/common/img-preview';
import popupDialog from '@/components/student/popup.vue';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview,
        dialogDelete,
        dialogSuccess,
        popupDialog
    },
    data () {
        return {
            formData: {
                popup_pos: 0,
                condition: 99,
                leavel: 99
            },
            posList: [{value: 0,label: '全部'},{value: 1,label: 'app加载时'},{value: 2,label: '商城模块'},{value: 4,label: '工作模块'},{value: 8,label: '社交模块'},{value: 16,label: '个人中心模块'}],
            condList: [{value: 99,label: '全部'},{value: 0,label: '直接展示'},{value: 1,label: '精英升级'}],
            leavelList: [{value: 99,label: '全部'},{value: 0,label: 'E0'},{value: 1,label: 'E1'},{value: 2,label: 'E2'},{value: 3,label: 'E3'},{value: 4,label: 'E4'},{value: 5,label: 'E5'}],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [
                {
                    group_show: true
                }
            ],
            authResult: {}
        };
    },
    filters: {
        filtertype (val) {
            let scope = {
                1: 'app加载时',
                2: '商城模块',
                4: '工作模块',
                8: '社交模块',
                16: '个人中心模块'
            };
            let str = '';

            if(!!val) {
                val.map(item => {
                    str += scope[item];
                });
            }

            return str;
        },
        filterLeavel (val) {
            if(val === 0) return 'E0';
            if(val === 1) return 'E1';
            if(val === 2) return 'E2';
            if(val === 3) return 'E3';
            if(val === 4) return 'E4';
            if(val === 5) return 'E5';
        },
        filterVerfy (val) {
            if(val === 0) return '待审核';
            if(val === 1) return '审核通过';
            if(val === -1) return '审核未通过';
        }
    },
    computed: {},
    watch: {},
    mounted () {
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/popup%5Cget_list', {
                page_id: page || this.page.now_page,
                style_type: 1,
                scope: this.formData.popup_pos,
                require_stuagent_level: this.formData.leavel,
                event_type: this.formData.condition
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 确定新增弹窗
         */
        confirmDialog (row) {
            if(row.type == 'add') {
                this.$post('/student/popup%5Cadd',{
                    style_type: 1,
                    image_url: row.qrcode,
                    scope: row.popup_pos,
                    event_type: row.condition,
                    require_stuagent_level: row.leavel
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }

            if(row.type == 'edit') {
                this.$post('/student/popup%5Cset',{
                    popup_no: row.popup_no,
                    image_url: row.qrcode,
                    scope: row.popup_pos,
                    event_type: row.condition,
                    require_stuagent_level: row.leavel
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }
        },

        /**
         * @description 确定删除弹窗
         */
        confrimDelete (row) {
            this.$post('/student/popup%5Cdelete',{
                popup_no: row.popup_no
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 通过弹窗
         */
        confrimSuccess (row) {
            this.$post('/student/popup%5Cverify',{
                popup_no: row.popup_no,
                verify: 1
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 不通过弹窗
         */
        async confrimReject (row) {
            let reason = await this.$zdPrompt({
                title: '审核驳回',
                message: '不通过原因',
                placeholder: '请输入不通过的原因...',
            });

            if(reason) {
                this.$post('/student/popup%5Cverify',{
                    popup_no: row.popup_no,
                    verify: -1,
                    verify_fail_reason: reason
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }

        },

        /**
         * @description 上架/下架
         */
        fnShowStatus (row) {
            this.$post('/student/popup%5Cset_status',{
                popup_no: row.popup_no,
                status: row.status
            },resp => {
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },


        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.zsdxPopupCode {
    background-color: #fff;
    display: flex;
    flex-direction: column;
    position: relative;
    margin: 0 32px;
    flex: 1;
    .header-nav {
        position: relative;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        margin-top: 24px;
        .header-left, .header-right {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }
        .header-new-box {
            display: flex;
            margin-right: 15px;
            align-items: center;
            color: #606266;
            margin-bottom: 24px;
            white-space: nowrap;
            &-title {
                margin-right: 2px;
                white-space: nowrap;
            }
        }
    }
    /* 表单 */
    .scope-price {
        width: 50px;
        height: 50px;
    }
    /* 字体高亮 */
    .cursor-font {
        color: rgb(64, 158, 255);
        cursor: pointer;
    }
    /* 分页 */
    .pagination-wrapper {
        padding-top: 10px;
        padding-bottom: 10px;
        .el-pagination {
            float: right;
            padding: 0;
            background-color: transparent;
        }
    }
}
/* 排序 */
.sort-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .sort-spliter {
        width: 1px;
        height: 12px;
        background: rgba(235, 235, 235, 1);
        margin: 0 10px;
        }
        .btn-sort {
        font-size: 14px;
        line-height: 1;
        color: #ccc;
        &:hover {
            color: #20a0ff;
        }
    }
}
.el-button-left {
    margin-left: 40px;
}
.el-button-right {
    margin-right: 10px;
}
.__flex {
    display: flex;
    align-items: center;
    .__flex_status {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        margin-right: 4px;
    }
}
</style>
<style lang="less">
    .zsdxPopupCode{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>